package com.cinematicketscaning.app.utils

import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.DatePicker
import android.widget.LinearLayout
import android.widget.NumberPicker
import androidx.fragment.app.FragmentManager
import com.cinematicketscaning.app.dialog.TipsDialog
import java.util.*

object Utils {

    fun showTwoBtnDialog(fragmentManager: FragmentManager, title: String, content: String,
                         leftStr: String, rightStr: String, clickEvent: TipsDialog.ClickEvent, cancelable: Boolean = true) {
        val dialog = TipsDialog()
        dialog.clickEvent = clickEvent
        dialog.title = title
        dialog.content = content
        dialog.leftStr = leftStr
        dialog.rightStr = rightStr
        dialog.isCancelable = cancelable
        dialog.show(fragmentManager, TipsDialog.TAG)
    }

    fun initCalendar(): Calendar {
        return Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"))
    }

    fun getFormatDay(day: Int): String {
        return getFormatMonth(day)
    }

    fun getFormatMonth(month: Int): String {
        val monthStr = month.toString()
        return if (monthStr.length < 2) {
            "0$monthStr"
        } else monthStr
    }
    /**
     * 设置时间选择器的分割线颜色
     *
     * @param datePicker
     */
    fun setDatePickerDividerColor(datePicker: DatePicker) {
        // Divider changing:

        // 获取 mSpinners
        val llFirst = datePicker.getChildAt(0) as LinearLayout

        // 获取 NumberPicker
        val mSpinners = llFirst.getChildAt(0) as LinearLayout
        for (i in 0 until mSpinners.childCount) {
            val picker = mSpinners.getChildAt(i) as NumberPicker

            val pickerFields = NumberPicker::class.java.declaredFields
            for (pf in pickerFields) {
                if (pf.name == "mSelectionDivider") {
                    pf.isAccessible = true
                    try {
                        pf.set(picker, ColorDrawable(Color.parseColor("#333333")))//设置分割线颜色
                    } catch (e: IllegalArgumentException) {
                        e.printStackTrace()
                    } catch (e: Resources.NotFoundException) {
                        e.printStackTrace()
                    } catch (e: IllegalAccessException) {
                        e.printStackTrace()
                    }
                    break
                }
            }
        }
    }
}